package test;

import java.util.ArrayList;
import java.util.List;

import com.oa.bean.FileInfo;

public class FileInfoTree {
    private List<FileInfo> FileInfoList = new ArrayList<FileInfo>();
    public FileInfoTree(List<FileInfo> FileInfoList) {
        this.FileInfoList=FileInfoList;
    }

    //建立树形结构
    public List<FileInfo> buildTree(){
        List<FileInfo> treeFileInfos =new  ArrayList<FileInfo>();
        for(FileInfo FileInfoNode : getRootNode()) {
            FileInfoNode=buildChildTree(FileInfoNode);
            treeFileInfos.add(FileInfoNode);
        }
        return treeFileInfos;
    }

    //递归，建立子树形结构
    private FileInfo buildChildTree(FileInfo pNode){
        List<FileInfo> chilFileInfos =new  ArrayList<FileInfo>();
        for(FileInfo FileInfoNode : FileInfoList) {
            if(FileInfoNode.getCarentId().equals(pNode.getFileId())) {
                chilFileInfos.add(buildChildTree(FileInfoNode));
            }
        }
        pNode.setChildren(chilFileInfos);
        return pNode;
    }

    //获取根节点
    private List<FileInfo> getRootNode() {
        List<FileInfo> rootFileInfoLists =new  ArrayList<FileInfo>();
        for(FileInfo FileInfoNode : FileInfoList) {
            if(FileInfoNode.getCarentId().equals(0)) {
                rootFileInfoLists.add(FileInfoNode);
            }
        }
        return rootFileInfoLists;
    }
}



